<?php
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
// DLMan by Shedd Technologies International		  				//
// http://www.dlman.com | info@dlman.com							//
// Copyright 2003 by STI, All rights reserved.						//
// ---------------------------------------------------------------- //
// Usage of this software is governed by the terms of GPL. 	    	//
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

/*
	Transaction Calendar Generation
	Shedd Technologies International
*/
require_once("global.php");

//--------------------------------------------------------------------

function calendar($month,$year){
	global $config,$PHP_SELF;
	ob_start();
	define("ADAY",(60*60*24));
	if(!checkdate($month,1,$year)){
		$nowArray=getdate();
		$month=$nowArray['mon'];
		$year=$nowArray['year'];
	}
	
	//stuff for next & back links
		//make time based on month & year
		$oldnow=mktime(0,0,0,$month,1,$year);
		$ONnowArray=getdate(time());
		//get dates for back link
		$monthagoArray=getdate(DateAdd("m",-1,$oldnow));//add -1 months to current date, get array for that date
		$pmonth=$monthagoArray["mon"];
		$pyear=$monthagoArray["year"];
		//get dates for next link
		$monthnextArray=getdate(DateAdd("m",1,$oldnow));//add 1 months to current date, get array for that date
		$nmonth=$monthnextArray["mon"];
		$nyear=$monthnextArray["year"];
	//END stuff for next & back links
	
	//generate calendar	
	$start=mktime(0,0,0,$month,1,$year);
	$firstDayArray=getdate($start);
	$days=array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	$dA=getdate($start);
	print "<TABLE border=0 cellspacing=1 cellpadding=5>\n";
	///////////////////////////////////////////////////////
	//MONTH HEADER
	print "\t<tr><td colspan=7 align=center>";
	//back link
	?><a href="<?php print $PHP_SELF; ?>?month=<?php print $pmonth; ?>&year=<?php print $pyear; ?>#cal">&lt;&lt;</a>&nbsp;<?php
	print "<b>$dA[month] $dA[year]</b>";
	//next link
		//only display next link if it is not the current year
		if($year<$ONnowArray['year']){
			?>&nbsp;<a href="<?php print $PHP_SELF; ?>?month=<?php print $nmonth; ?>&year=<?php print $nyear; ?>#cal">&gt;&gt;</a><?php
		}
		else{
			//if is the current year, check month
			if($month!=$ONnowArray['mon']){
				?>&nbsp;<a href="<?php print $PHP_SELF; ?>?month=<?php print $nmonth; ?>&year=<?php print $nyear; ?>">&gt;&gt;</a><?php
			}
		}
	print "</td></tr>\n";
	//end MONTH HEADER
	///////////////////////////////////////////////////////
	foreach($days as $day){
		print "\t<td align=center><b>$day</b></td>\n";
	}
	///////////////////////////////////////////////////////
	for($count=0;$count<(6*7);$count++){
		$dayArray=getdate($start);
		if(($count%7)==0){
			if($dayArray['mon']!=$month){
				break;
			}
			print "</tr><tr>\n";
		}
		if($count<$firstDayArray['wday']||$dayArray['mon']!=$month){
			print "\t<td><br></td>\n";
		}
		else{
			//is it today?
			$nowArray=getdate(time());
			//CELL CREATION
			//today
			if($dayArray['year']==$nowArray['year']&&$dayArray['mday']==$nowArray['mday']&&$dayArray['mon']==$nowArray['mon']){
				print "\t<td align=right bgcolor=#e5e5e5 class=today>";
			}
			//other cases
			else{
				//already passed, not this year
				if($dayArray['year']!=$nowArray['year']){
					print "\t<td align=right bgcolor=#d0d0d0>";
				}
				//already passed, this year
				elseif($dayArray['year']==$nowArray['year']&&$dayArray['mon']==$nowArray['mon']&&$dayArray['mday']<$nowArray['mday']){
					print "\t<td align=right bgcolor=#d0d0d0>";
				}
				//future
				elseif($dayArray['year']==$nowArray['year']&&$dayArray['mday']>$nowArray['mday']&&($dayArray['mon']==$nowArray['mon'])){
					print "\t<td align=right bgcolor=#f1f1f1>";
				}
				else{
					print "\t<td align=right bgcolor=#d0d0d0>";
				}
			}
			
			print "<font size=1>";
			//get transaction data for the current day
			unset($dtot);
			unset($num);
			unset($result);
			unset($value);
			unset($bounds);
			$bounds=bound($dayArray['mday'],$dayArray['mon'],$dayArray['year']);
			$sql="SELECT txn_id,cost,timestamp FROM ".$config->dt['transactions'].", ".$config->dt['files']." WHERE ".$config->dt['transactions'].".file_id = ".$config->dt['files'].".id AND timestamp>'$bounds[0]' AND timestamp<'$bounds[1]' AND ".$config->dt['transactions'].".status='ACTIVE'";
			$result=@mysql_query($sql);
			$num=@mysql_num_rows($result);
			//day data
			$dtot=0;
			if($num>0){
				while($value=@mysql_fetch_array($result)){
					$dtot+=$value['cost'];
				}//end while
				//print day # with link
				$link_code=$bounds[0]."_".$bounds[1];
				print "<a onmouseout='return dstatus(\"\")' onmouseover='return dstatus(\"View Detailed Transaction Data (new window)\")' href='#cal' onclick=\"javascript: window.open('tdetail.php?trans_id=$link_code', 'newwindow', config='height=400, width=400, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no')\">$dayArray[mday]</a><br>";
			}//end more than 0
			//print plain day #
			else{
				print "$dayArray[mday]<br>";
			}
			print "$num Transaction(s)<br>";
			if($dtot>0){
				print "<b>\$".sprintf("%.2f",$dtot)."</b>";
			}
			else{
				print "\$".sprintf("%.2f",$dtot);
			}
			print "</font>";
			print "</td>\n";
			$start+=ADAY;
		}
	}
	print "</tr></TABLE>";
	$final=ob_get_contents();
	ob_end_clean();
	return $final;
}//end function

//--------------------------------------------------------------------

//generate the upper and lower bounds for a date
//	return array[lower,upper]
function bound($day,$month,$year){
	//make time based on month & year
	$lower=mktime(0,0,0,$month,$day,$year);
	$monthnextArray=getdate(DateAdd("d",1,$lower));//add 1 day to current date, get array for that date
	$upper=mktime(0,0,0,$monthnextArray["mon"],$monthnextArray["mday"],$monthnextArray["year"]);
	//store result to array
	$result=array($lower,$upper);
	return $result;
}//end bound function
?>
